#!/usr/bin/env python 3.6
# -*- coding: utf-8 -*-
# @Time      :2019/2/13
# @Author    :Tian Maohong
# @Email     :tianmaohong@bbdservice.com
# @File      :R38_EC_UpaidCreditBusinessArrearsInterestCntCheck
# @Software  :PyCharm

'''eof
name:申请企业未结清信贷业务当前欠息笔数校验
code:R38_EC_UpaidCreditBusinessArrearsInterestCntCheck
tableName:
columnName:
groups:企业征信校验
dependencies:EC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''




import re
import sys
import datetime



reload(sys)
sys.setdefaultencoding('utf-8')


null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None


def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False



def upaidCreditBusinessArrearsInterestCntCheck():
    """
    Factor=max（未结清信贷信息概要(资产处置、欠息)（ECRSummaryUnsettled01）
    中取"欠息汇总（笔数）"（OweBalancesum_Number），
    未结清欠息（ECRUnpaidDebtInterest）中统计"信用报告编号"的数量）
    :return:
    """
    ECRSummaryUnsettled01 = getTable('ECRSummaryUnsettled01')
    ECRUnpaidDebtInterest = getTable('ECRUnpaidDebtInterest')

    if ECRSummaryUnsettled01 is None and ECRUnpaidDebtInterest is None:
        return u'缺失值'
    else:
        if ECRSummaryUnsettled01 == [{}]:
            result1 = 0
        else:
            OweBalancesum_Number = ECRSummaryUnsettled01[0].get('OweBalancesum_Number')
            if isfloat(OweBalancesum_Number):
                result1 = float(OweBalancesum_Number)
            else:
                result1 = None

        if ECRUnpaidDebtInterest == [{}]:
            result2 = 0
        else:
            tmp = []
            for i in ECRUnpaidDebtInterest:
                reportNo = i.get('ReportNo')
                if reportNo in null_type_list:
                    tmp.append(None)
                else:
                    tmp.append(1)
            tmp = filter(lambda x:x is not None, tmp)
            if len(tmp) ==0:
                result2 = None
            else:
                result2 = sum(tmp)
        if result1 is None and result2 is None:
            return u'缺失值'
        else:
            if result1 is None:
                result1 = 0
            if result2 is None:
                result2 = 0
            return max(result1, result2)

result = upaidCreditBusinessArrearsInterestCntCheck()

